package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.rock.TaskService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author wuguangxi
 * @Description
 * @Date 2024/3/14 14:37
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/task_info")
public class TaskController2 {
    @Autowired
    private TaskService taskService;

    @RestGrpcMapping("/findRelateTaskByTask.do")
    public NTSTATUS findRelateTaskByTaskId(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            TaskQueryParams taskQueryParams = params.driverObject(TaskQueryParams.class);
            result.setData(taskService.findByTaskIdOrNaneAndType(taskQueryParams.getTask(), taskQueryParams.getRelationType()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/getTaskScore.do")
    public NTSTATUS getTaskScore(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}, user: {}", params, params.userID);
        try {
            TaskQueryParams taskQueryParams = params.driverObject(TaskQueryParams.class);
            result.setData(taskService.getTaskScore(taskQueryParams.getTask()));
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @Data
    private static class TaskQueryParams {

        String task;

        String relationType;
    }
}
